#pragma once

#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t* lock_p=nullptr)
        :_lock_p(lock_p)
    {}

    void lock()
    {
        //如果真的传入了互斥量，不为空，加锁
        if(_lock_p) pthread_mutex_lock(_lock_p);
    }

    void unlock()
    {
        if(_lock_p) pthread_mutex_unlock(_lock_p);
    }

    ~Mutex()
    {}
private:
    pthread_mutex_t *_lock_p;   //互斥量指针
};

class LockGuard
{
public:
    //构造时，自动加锁
    LockGuard(pthread_mutex_t* lock)
        :_mutex(lock)   //构造Mutex类对象
    {
        _mutex.lock();
    }

    //销毁时，自动解锁
    ~LockGuard()
    {
        _mutex.unlock();
    }
private:
    Mutex _mutex;
};